<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">

      <h1 class="title">{{ "免保模拟器" }}
          <img src="https://game.gtimg.cn/images/dfm/cp/a20240729directory/bxx.png" alt="" :style="{background:getColor(6),width:'50px',height: 'auto'}">
      </h1>

      <div class="title" >
        <img src="../../public/img/zb.png" alt="" style="width: 100px;height: auto" v-if="countM===0">
        <img src="../../public/img/dk.png" alt="" style="width: 100px;height: auto" v-if="countM > 0 && countM<20000">
        <img src="../../public/img/sq.jpg" alt="" style="width: 100px;height: auto" v-if="countM>20000 && countM <50000">
        <img src="../../public/img/hh.png" alt="" style="width: 100px;height: auto" v-if="countM>50000 && countM<100000">
        <img src="../../public/img/jy.png" alt="" style="width: 100px;height: auto" v-if="countM>100000 && countM<200000">
        <img src="../../public/img/dc.png" alt="" style="width: 100px;height: auto" v-if="countM>200000">
      </div>
      <el-table :data="list" :show-header="false" border :cell-style="getCellStyle" :span-method="spanMethed" :key="tableKey">
        <el-table-column label="name" align="center" prop="11" width="100">
          <template #default="scope">
            <div v-if="scope.row.list && scope.row.list[0] && !imgLoading[0]">
              <span class="text" style="font-size: 12px;padding: 10px">{{ scope.row.list[0].name }}</span>
              <span class="text" style="font-size: 12px;right: 0;bottom: 0;padding: 10px">{{ scope.row.list[0].avgPrice }}</span>
              <img :src="scope.row.list[0].pic" :style="{width:(scope.row.list[0].length* 100) + 'px',height: (scope.row.list[0].width * 100) + 'px',background: getColor(scope.row.list[0].grade)}"  alt="">
            </div>
            <div v-else-if="scope.row.list && scope.row.list[0] && imgLoading[0]">
              <el-icon class="is-loading"  v-if="touchLoading[0]"  style="position: absolute; z-index: 99;
                  top: 46%; left: 46%;
                  transform: translate(-50%, -50%);">
                <Loading />
              </el-icon>
              <img src="../../public/img/w.jpg" :style="{width:(scope.row.list[0].length* 100) + 'px',height: (scope.row.list[0].width * 100) + 'px'}"  alt="" >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="name" align="center" prop="11" width="100">
          <template #default="scope">
            <div v-if="scope.row.list && scope.row.list[1] && !imgLoading[1]">
              <span class="text" style="font-size: 12px;padding: 10px">{{ scope.row.list[1].name }}</span>
              <span class="text" style="font-size: 12px;right: 0;bottom: 0;padding: 10px">{{ scope.row.list[1].avgPrice }}</span>
              <img :src="scope.row.list[1].pic" :style="{width:(scope.row.list[1].length* 100) + 'px',height:(scope.row.list[1].width * 100) + 'px',background: getColor(scope.row.list[1].grade)}"  alt="">
            </div>
            <div v-else-if="scope.row.list && scope.row.list[1] && imgLoading[1]">
              <el-icon class="is-loading"  v-if="touchLoading[1]"  style="position: absolute; z-index: 99;
                  top: 46%; left: 46%;
                  transform: translate(-50%, -50%);">
                <Loading />
              </el-icon>
              <img src="../../public/img/w.jpg" :style="{width:(scope.row.list[1].length* 100) + 'px',height: (scope.row.list[1].width * 100) + 'px'}"  alt="">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="name" align="center" prop="11" width="100">
          <template #default="scope">
            <div v-if="scope.row.list && scope.row.list[2] && !imgLoading[2]">
              <span class="text" style="font-size: 12px;padding: 10px">{{ scope.row.list[2].name }}</span>
              <span class="text" style="font-size: 12px;right: 0;bottom: 0;padding: 10px">{{ scope.row.list[2].avgPrice }}</span>
              <img :src="scope.row.list[2].pic" :style="{width:(scope.row.list[2].length* 100) + 'px',height:(scope.row.list[2].width * 100) + 'px',background: getColor(scope.row.list[2].grade)}"  alt="">
            </div>
            <div v-else-if="scope.row.list && scope.row.list[2] && imgLoading[2]" >
              <el-icon class="is-loading"  v-if="touchLoading[2]"  style="position: absolute; z-index: 99;
                  top: 46%; left: 46%;
                  transform: translate(-50%, -50%);">
                <Loading />
              </el-icon>
              <img src="../../public/img/w.jpg" :style="{width:(scope.row.list[2].length* 100) + 'px',height: (scope.row.list[2].width * 100) + 'px'}"  alt="">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="name" align="center" prop="11" width="100">
          <template #default="scope">
            <div v-if="scope.row.list && scope.row.list[3] && !imgLoading[3]">
              <span class="text" style="font-size: 12px;padding: 10px">{{ scope.row.list[3].name }}</span>
              <span class="text" style="font-size: 12px;right: 0;bottom: 0;padding: 10px">{{ scope.row.list[3].avgPrice }}</span>
              <img :src="scope.row.list[3].pic" :style="{width:(scope.row.list[3].length* 100) + 'px',height:(scope.row.list[3].width * 100) + 'px',background: getColor(scope.row.list[3].grade)}"  alt="">
            </div>
            <div v-else-if="scope.row.list && scope.row.list[3] && imgLoading[3]">
              <el-icon class="is-loading"  v-if="touchLoading[3]"  style="position: absolute; z-index: 99;
                  top: 46%; left: 46%;
                  transform: translate(-50%, -50%);">
                <Loading />
              </el-icon>
              <img src="../../public/img/w.jpg" :style="{width:(scope.row.list[3].length* 100) + 'px',height: (scope.row.list[3].width * 100) + 'px'}"  alt="">
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-form-item style="width:100%;" v-if="countM!==0">
        <h3 class="title"> 价值 【{{ countM }} 】</h3>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
            :loading="loading"
            size="large"
            type="primary"
            style="width:100%;"
            @click="touch()"
        >
          <span v-if="!loading">开 摸！</span>
          <span v-else>偷吃中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import {getColor, randTouch} from "@/utils/sjz/common.js";
const countM = ref(0)
const tableKey = ref(0)
const loading = ref(false)
const imgLoading = ref([false,false,false,false])
const touchLoading = ref([false,false,false,false])
const list = ref([
  {name:"11",garde:""},
  {name:"11",garde:""},
  {name:"11",garde:""},
  {name:"11",garde:""}
])
function touch() {
  countM.value = 0
  loading.value = true;
  imgLoading.value = [true, true, true, true]; // 初始4个元素
  randTouch().then(res => {
    list.value = res.data;
    // loading.value = false;
    console.log("lsit",list.value)
    // 调用递归函数，从第0个元素开始处理
    recursiveShow(0);

  }).catch(err => {
    loading.value = false;
    console.error("请求失败：", err);
  });
}

// 递归函数：index为当前要处理的元素索引
function recursiveShow(index) {
  // 递归终止条件：如果索引超出范围，停止递归
  if (index === list.value[0].list.length) {
    loading.value = false
    return;
  }
  const obj = list.value[0].list[index]

  const dealTime = getTouchTimer(obj.grade)
  touchLoading.value[index] = true
  // 延迟2秒后处理当前元素，并递归处理下一个
  setTimeout(() => {
    // 标记当前元素为加载完成（显示）
    imgLoading.value[index] = false;
    touchLoading.value[index] = false
    // tableKey.value++;
    countM.value+=obj.avgPrice
    // 递归处理下一个元素（索引+1）
    recursiveShow(index + 1);
  }, dealTime); // 每个元素间隔2秒显示
}

function getTouchTimer(garde){
  console.log(garde)
  if (garde<=4){
    return 1000
  }else if(garde===5){
    return 2000
  }else {
    return 3000
  }
}

function getCellStyle(){
  let cell = {
    boxSizing: 'border-box',

    backgroundColor: 'rgb(240, 240, 240)',
    height: '100px',
    padding: '0'
  }
  return cell
}

const spanMethed = ({
                           row,
                           column,
                           rowIndex,
                           columnIndex,
                         }) => {

  if (row.list && row.list[columnIndex]) {
    if (row.list[columnIndex]) {
      return {
        rowspan:  row.list[columnIndex].width,
        colspan: row.list[columnIndex].length ,
      }
    } else{
      return {
        rowspan: 1,
        colspan: 1,
      }
    }
  }
}


</script>

<style lang='scss' scoped>
:deep(.el-table .cell){
  line-height: 0;
  padding: 0 !important;
}
:deep(.el-table){
  --el-table-border-color: #dddddd
}
img{
  z-index: 1;
}
.text{
  background-color: rgba(0, 0, 0, 0.5);
  font-size: 10px;
  color: white;
  position: absolute;
  z-index: 3;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  //background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 450px;
  padding: 25px 25px 5px 25px;
  z-index: 1;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}
.example-showcase .el-loading-mask {
  z-index: 9;
}
</style>
